

export function form2Table(form, tableData, filter='parameter') {
  const splitData = Object.keys(form).reduce((acc, key) => {
    if (key in tableData[0] && key !== filter) {
      acc[key] = form[key].split('|');
    }
    return acc;
  }, {});
  
  tableData.forEach((item, index) => {
    Object.keys(splitData).forEach(key => {
      item[key] = splitData[key][index];
    });
  });
}

export function table2Form(tableData, form, filter='parameter') {
  const formData = tableData.reduce((acc, cur) => {
    Object.keys(cur).forEach(key => {
      if (key !== filter) {
        acc[key] = acc[key] ? `${acc[key]}|${cur[key]}` : cur[key];
      }
    });
    return acc;
  }, {})

  Object.keys(formData).forEach(key => {
    form[key] = formData[key];
  });
}

export function table2FormContain(tableData, form, pattern='') {
  const formData = tableData.reduce((acc, cur) => {
    Object.keys(cur).forEach(key => {
      if (pattern.length > 0 && key.indexOf(pattern) > -1) {
        acc[key] = acc[key] ? `${acc[key]}|${cur[key]}` : cur[key];
      }
    });
    return acc;
  }, {})

  Object.keys(formData).forEach(key => {
    form[key] = formData[key];
  });
}

  export function table2FormExclude(tableData, form, filters=[]) {
    const formData = tableData.reduce((acc, cur) => {
      Object.keys(cur).forEach(key => {
        if (!filters.includes(key)) {
          acc[key] = acc[key] ? `${acc[key]}|${cur[key]}` : cur[key];
        }
      });
      return acc;
    }, {})
  
    Object.keys(formData).forEach(key => {
      form[key] = formData[key];
    });
}
